<template>
  <div>
    <div style="margin: 10px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="手机号">
          <el-input v-model="searchObj.mobile" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="searchObj.userType" placeholder="请选择">
            <el-option label="投资人" value="1"></el-option>
            <el-option label="借款人" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-select v-model="searchObj.status" placeholder="请选择">
            <el-option label="锁定" value="0"></el-option>
            <el-option label="正常" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >查询</el-button
          >
          <el-button @click="handleClear">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 展示会员列表的表格 -->
    <div>
      <el-table :data="list" border style="width: 100%">
        <el-table-column label="序号" width="40" type="index">
        </el-table-column>
        <el-table-column prop="userType" label="用户类型" width="80">
          <template slot-scope="scope">
            <el-tag
              v-if="Number(scope.row.userType) === 1"
              type="success"
              effect="dark"
            >
              投资人
            </el-tag>
            <el-tag
              v-else-if="Number(scope.row.userType) === 2"
              type="warning"
              effect="dark"
            >
              借款人
            </el-tag>
            <el-tag v-else type="info" effect="dark"> 未选择 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="name" label="用户姓名" width="80">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号"> </el-table-column>
        <el-table-column prop="integral" label="用户积分" width="60">
        </el-table-column>
        <el-table-column prop="createTime" label="注册时间"> </el-table-column>
        <el-table-column prop="bindStatus" label="绑定状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.bindStatus) === 1" type="success">
              已绑定
            </el-tag>
            <el-tag
              v-else-if="Number(scope.row.bindStatus) === -1"
              type="warning"
            >
              绑定失败
            </el-tag>
            <el-tag v-else type="info"> 未绑定 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="用户状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.status) === 1" type="success">
              正常
            </el-tag>
            <el-tag v-else type="warning"> 锁定 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :type="scope.row.status === 1 ? 'warning' : 'success'"
              @click="changeLock(scope.row)"
              >{{ scope.row.status === 1 ? "锁定" : "解锁" }}
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleQueryLoginRecords(scope.$index, scope.row)"
              >登录日志</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页导航 -->
    <div style="text-align: center; margin-top: 30px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 3, 4, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="用户登录日志" :visible.sync="dialogTableVisible">
      <el-table :data="loginRecords">
        <el-table-column
          type="index"
          label="序号"
          width="100"
        ></el-table-column>
        <el-table-column property="ip" label="IP"></el-table-column>
        <el-table-column
          property="createTime"
          label="登录时间"
        ></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import userInfoApi from "@/api/userInfo";
export default {
  data() {
    return {
      dialogTableVisible: false,
      searchObj: {},
      pageNum: 1,
      pageSize: 3,
      list: [],
      total: 0,
      loginRecords: [],
    };
  },
  created() {
    this.initData();
  },
  methods: {
    //分页导航的事件函数：
    //pageSize改变时回调：传入最新的pageSize值
    handleSizeChange(size) {
      console.log("handleSizeChange", size);
      this.pageSize = size;
      this.initData();
    },
    //pageNUm改变时回调：传入最新的页码
    handleCurrentChange(num) {
      console.log("handleCurrentChange", num);
      this.pageNum = num;
      this.initData();
    },
    //查询登录日志按钮事件函数
    handleQueryLoginRecords(index, row) {
      userInfoApi.listLoginRecords(row.id).then((r) => {
        // console.log(r.data.items);
        this.loginRecords = r.data.items;
        //弹出对话框
        this.dialogTableVisible = true;
      });
    },
    //锁定解锁 会员的按钮事件函数
    changeLock(row) {
      console.log(row);
      userInfoApi.changeLock(row.id, row.status === 0 ? 1 : 0).then((r) => {
        this.$message.success(row.status === 0 ? "解锁成功" : "锁定成功");
        //重新刷新页面数据
        this.initData();
      });
    },
    //查询按钮事件函数
    handleQuery() {
      this.initData();
    },
    //清空按钮事件函数
    handleClear() {
      this.searchObj = {};
      this.pageNum = 1;
      this.pageSize = 3;
      this.initData();
    },
    //加载分页数据展示
    initData() {
      userInfoApi
        .listPage(this.pageNum, this.pageSize, this.searchObj)
        .then((r) => {
          //r代表响应体中的R对象： utils/request.js配置了响应拦截器
          //list用来接收查询到的分页数据列表
          this.list = r.data.page.records;
          this.total = Number(r.data.page.total);
        });
    },
  },
};
</script>
